package com.ns.dcjh.ui.popup;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.google.gson.Gson;
import com.lxj.xpopup.impl.PartShadowPopupView;
import com.ns.dcjh.R;
import com.ns.dcjh.bean.FullScreen;
import com.ns.dcjh.enums.TextColorEnum;
import com.ns.dcjh.utils.DisplayUtil;
import com.ns.dcjh.utils.FileUtils;

import java.text.DecimalFormat;

public class FullScreenSettingPopup extends PartShadowPopupView {

    public static final int BACKGROUND_BLACK = 1001;
    public static final int BACKGROUND_WHITE = 1002;

    public static final int TEXT_COLOR_WHITE = 1011;
    public static final int TEXT_COLOR_GRAY = 1012;
    public static final int TEXT_COLOR_ORANGE = 1013;
    public static final int TEXT_COLOR_RED = 1014;
    public static final int TEXT_COLOR_BLUE = 1015;
    public static final int TEXT_COLOR_GREEN = 1016;
    public static final int TEXT_COLOR_BLACK = 1017;

    private LinearLayout bgRoot;
    private RadioGroup bgRadioGroup;
    private RadioButton fullSettingBgBtnBlack;
    private RadioButton fullSettingBgBtnWhite;

    private RadioGroup textColorRadioGroup;

    private SeekBar fullScreenTextSizeSb;
    private TextView fullScreenSizeText;

    private SeekBar fullScreenCutDownSb;
    private TextView fullScreenCutDownText;

    private SeekBar fullScreenSpeedSb;
    private TextView fullScreenSpeedText;


    private Switch fullScreenReverseSwitch;

    private LinearLayout btnRecover;

    private FullScreen fullScreen;

    private boolean isLandscape;

//    private LinearLayout

    private OnFullScreenPopupChangeListener onFullScreenPopupChangeListener;

    public FullScreenSettingPopup(@NonNull Context context, FullScreen fullScreen,boolean isLandscape) {
        super(context);
        this.fullScreen =fullScreen;
        this.isLandscape = isLandscape;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        initView();

        /**
         * 字体大小设置
         */
        fullScreenTextSizeSb.setProgress((int)(fullScreen.textSize-20)); //设置默认35sp
        fullScreenSizeText.setText(((int)fullScreen.textSize)+"");
        fullScreenTextSizeSb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                int size = 20+progress;
                fullScreenSizeText.setText(String.valueOf(size));
                onFullScreenPopupChangeListener.onChangeTextSize(size);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        /**
         * 倒计时设置
         */
        fullScreenCutDownSb.setProgress(fullScreen.cutDownTime); //设置默认倒计时2s
        fullScreenCutDownText.setText(fullScreen.cutDownTime+"");
        fullScreenCutDownSb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                fullScreenCutDownText.setText(String.valueOf(progress));
                onFullScreenPopupChangeListener.onChangeCutDownTime(progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        /**
         * 速度设置
         */
        fullScreenSpeedSb.setProgress((int)(fullScreen.speed*10)-1);
        fullScreenSpeedText.setText(fullScreen.speed+"");
        fullScreenSpeedSb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                int speedValue = progress+1;
                float speed = ((float) speedValue) /10;
                DecimalFormat fnum= new DecimalFormat("##0.0");
                String speedStr=fnum.format(speed);
                fullScreenSpeedText.setText(speedStr);
                onFullScreenPopupChangeListener.onChangeSpeed(speed);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });



        /**
         * 字体颜色设置
         */
        textColorRadioGroup.removeAllViews();
        for (TextColorEnum e: TextColorEnum.values()){
            RadioButton radioButton = (RadioButton) View.inflate(getContext(), R.layout.layout_raido_button, null);
            radioButton.setId(e.getColorType());
            radioButton.setText(e.getColorName());

            RadioGroup.LayoutParams lp = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            //设置 meary 宽度
            lp.setMargins(DisplayUtil.dip2px(getContext(), 6),
                    DisplayUtil.dip2px(getContext(), 2),
                    DisplayUtil.dip2px(getContext(), 6),
                    DisplayUtil.dip2px(getContext(), 2));


            if (e.getColorType() == fullScreen.textColorType){
                radioButton.setChecked(true);
            }

            textColorRadioGroup.addView(radioButton,lp);
        }
        textColorRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                onFullScreenPopupChangeListener.onChangeTextColor(radioGroup.getCheckedRadioButtonId());
            }
        });

        /**
         * 背景设置
         */
        if (fullScreen.background==BACKGROUND_BLACK){
            fullSettingBgBtnBlack.setChecked(true);
        }else {
            fullSettingBgBtnWhite.setChecked(true);
        }

        bgRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                int type = -1;
                if (radioGroup.getCheckedRadioButtonId() == R.id.fullSettingBgBtnBlack){
                    type = BACKGROUND_BLACK;
                }else if (radioGroup.getCheckedRadioButtonId() == R.id.fullSettingBgBtnWhite){
                    type = BACKGROUND_WHITE;
                }
                onFullScreenPopupChangeListener.onChangeBackgroundColor(type);
            }
        });

        /**
         * 设置镜像
         */
        if (fullScreen.isTextMirror==1){
            fullScreenReverseSwitch.setChecked(true);
        }else {
            fullScreenReverseSwitch.setChecked(false);
        }
        fullScreenReverseSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                onFullScreenPopupChangeListener.onChangeSwitchReverseText(b);
            }
        });


        btnRecover.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                fullScreen = new Gson().fromJson(FileUtils.getAssetsJson("fullScreen.json",getContext()),FullScreen.class);
                fullScreenTextSizeSb.setProgress((int)(fullScreen.textSize-20)); //设置默认35sp
                fullScreenSizeText.setText(((int)fullScreen.textSize)+"");
                fullScreenCutDownSb.setProgress(fullScreen.cutDownTime); //设置默认倒计时2s
                fullScreenCutDownText.setText(fullScreen.cutDownTime+"");
                fullScreenSpeedSb.setProgress((int)(fullScreen.speed*10)-1);
                fullScreenSpeedText.setText(fullScreen.speed+"");

                if (fullScreen.background==BACKGROUND_BLACK){
                    fullSettingBgBtnBlack.setChecked(true);
                }else {
                    fullSettingBgBtnWhite.setChecked(true);
                }

                if (fullScreen.isTextMirror==1){
                    fullScreenReverseSwitch.setChecked(true);
                }else {
                    fullScreenReverseSwitch.setChecked(false);
                }
                textColorRadioGroup.removeAllViews();
                for (TextColorEnum e: TextColorEnum.values()){
                    RadioButton radioButton = (RadioButton) View.inflate(getContext(), R.layout.layout_raido_button, null);
                    radioButton.setId(e.getColorType());
                    radioButton.setText(e.getColorName());

                    RadioGroup.LayoutParams lp = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    //设置 meary 宽度
                    lp.setMargins(DisplayUtil.dip2px(getContext(), 6),
                            DisplayUtil.dip2px(getContext(), 2),
                            DisplayUtil.dip2px(getContext(), 6),
                            DisplayUtil.dip2px(getContext(), 2));


                    if (e.getColorType() == fullScreen.textColorType){
                        radioButton.setChecked(true);
                    }

                    textColorRadioGroup.addView(radioButton,lp);
                }


                onFullScreenPopupChangeListener.onRecover();
            }
        });

    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.layou_full_screen_setting;
    }

    private void initView(){
        fullScreenTextSizeSb = findViewById(R.id.fullScreenTextSizeSb);
        fullScreenSizeText = findViewById(R.id.fullScreenSizeText);

        fullScreenCutDownSb = findViewById(R.id.fullScreenCutDownSb);
        fullScreenCutDownText = findViewById(R.id.fullScreenCutDownText);

        fullScreenSpeedSb = findViewById(R.id.fullScreenSpeedSb);
        fullScreenSpeedText = findViewById(R.id.fullScreenSpeedText);

        bgRoot = findViewById(R.id.fullSettingBgRoot);
        bgRadioGroup = findViewById(R.id.fullSettingBgRg);
        fullSettingBgBtnBlack = findViewById(R.id.fullSettingBgBtnBlack);
        fullSettingBgBtnWhite = findViewById(R.id.fullSettingBgBtnWhite);

        textColorRadioGroup = findViewById(R.id.fullSettingTextColorRg);

        fullScreenReverseSwitch = findViewById(R.id.fullScreenReverseSwitch);

        btnRecover = findViewById(R.id.fullScreenRecoverBtn);
    }




    public void setOnFullScreenPopupChangeListener(OnFullScreenPopupChangeListener onFullScreenPopupChangeListener){
        this.onFullScreenPopupChangeListener = onFullScreenPopupChangeListener;
    }

}
